Random numbers in standard C++ |
| The statistic properties of the numbers generated by the function rand() can be improved by using the CPP standard instead. This library includes several options to adjust the distribution of the random generator. Las propiedades estadísticas de los números generados por la función rand() pueden ser mejoradas usando la librería CPP estándar en su lugar. Esta librería incluye varias opciones para ajustar la distribución del generador aleatorio. |
| Problem 1 |
| Create a program called Play to generate 10 integer random values from 0 to 9 using a probabilistic uniform distribution. You need to insert a textbox called tbxOutput for this program. Do not forget to set the properties of the textbox: multi-line and read-only. Cree un programa llamado Play para generar 10 valores aleatorios enteros desde 0 a 9 usando una distribución de probabilidad uniforme. Usted necesita insertar una caja de texto llamada tbxOutput para este programa. No se olvide de fijar las propiedades de multi-line y read-only en la caja de texto. |

| Play.h |
| #pragma once //______________________________________ Play.h #include "resource.h" class Play: public Win::Dialog { public: Play() { random_device rd; randomGenerator.seed(rd()); } ~Play() { } // ___________________________________________ Linear congruential generator //std::minstd_rand randomGenerator; //___________________________________________ Mersenne twister engine std::mt19937 randomGenerator; protected: . . . }; |
| Play.cpp |
| . . . void Play::Window_Open(Win::Event& e) { //___________________________________________ Uniform integer distribution wchar_t text[32]; std::uniform_int_distribution<int> distribution(0, 9); // [0 9] int x = 0; for(int i = 0; i < 10; ++i) { x = distribution(randomGenerator); _snwprintf_s(text, 32, _TRUNCATE, L"%d\r\n", x); tbxOutput.Text += text; } } |
| Tip |
| The libraries to generate random numbers are included in the file called random that it is automatically included by all Wintempla projects. Las librerías para números aleatorios están incluidas en el archivo llamado random que es automáticamente incluido en todos los proyectos de Wintempla. |
| Problem 2 |
Create a program called PlayDouble to generate 2000 floating point random using a normal distribution.
Cree un programa llamado PlayDouble para generar 2000 números de punto flotante usando una distribución de probabilidad normal.
|



| PlayDouble.h |
| #pragma once //______________________________________ PlayDouble.h #include "resource.h" class PlayDouble: public Win::Dialog { public: PlayDouble() { randomGenerator.seed(::GetTickCount()); } ~PlayDouble() { } std::mt19937 randomGenerator; void RefreshHistogram(); protected: . . . }; |
| PlayDouble.cpp |
| . . . void PlayDouble::Window_Open(Win::Event& e) { this->tbxMean.DoubleValue = 40.0; this->tbxVariance.DoubleValue = 5.0; RefreshHistogram(); } void PlayDouble::tbxVariance_Change(Win::Event& e) { RefreshHistogram(); } void PlayDouble::tbxMean_Change(Win::Event& e) { RefreshHistogram(); } void PlayDouble::RefreshHistogram() { valarray<double> data(2000); const double mean = tbxMean.DoubleValue; const double variance = tbxVariance.DoubleValue; if (variance == 0.0) return; std::normal_distribution<double> distribution(mean, variance); for(int i = 0; i<2000; i++) { data[i] = distribution(randomGenerator); } histOutput.MinX = mean-20.0*sqrt(variance); histOutput.MaxX = mean+20.0*sqrt(variance); const double maxFreq = histOutput.SetData(data, 40, false); histOutput.MinY = 0.0; histOutput.MaxY = maxFreq; histOutput.CaptionY = L"Frequency"; } |
| Tip |
| The table below presents briefly some of the probability distributions supported. La tabla de abajo presenta en forma breve algunas de las distribuciones de probabilidad incluidas. |
| Name | Example |
| Integer Uniform | std::uniform_int_distribution |
| Double Uniform | std::uniform_real_distribution |
| Normal | std::normal_distribution |
| Bernoulli | std::bernoulli_distribution |
| Binomial | std:: binomial_distribution |
| Exponential | std::exponential_distribution |
| Gamma | std::gamma_distribution |
| Geometric | std::geometric_distribution |
| Poisson | std::poisson_distribution |